<template>
  <div class="page-container">
    <div class="toolbar" style="padding-top:20px;padding-left:15px;">
      <el-form :inline="true" :model="filters" ref="filters" :size="size">
        <el-row class="search-button">
          <el-form-item>
            <kt-button
              icon="fa fa-plus"
              :label="$t('action.add')"
              type="primary"
              @click="handleAdd"
            />
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <el-row class="t_r">
      <el-button-group>
        <el-tooltip content="刷新" placement="top">
          <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
        </el-tooltip>
        <el-tooltip content="列显示" placement="top">
          <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
        </el-tooltip>
      </el-button-group>
    </el-row>
    <div class="toolbar" style="float:right;padding-right:15px;">
      <!--表格显示列界面-->
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
    <cy-table
      :height="350"
      :highlightCurrentRow="true"
      :content="pageResult"
      :columns="filterColumns"
      :showSelection="false"
      :totalSize="totalSize"
      :showBatchDelete="false"
      :showDeleteOperation="false"
      @findPage="findPage"
      @handleEdit="handleEdit"
      ref="CyTable"
    ></cy-table>
    <!--新增编辑界面-->
    <el-dialog
      :title="operation?'新增':'编辑'"
      width="40%"
      :visible.sync="editDialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        label-width="130px"
        :rules="dataFormRules"
        ref="dataForm"
        :size="size"
      >
        <el-row class="searchDialog-input">
            <el-form-item label="售后类型：" prop="afterSaleType">
          <el-select v-model="selectOpt" placeholder="请选择" style="width: 100%">
            <el-option label="退款已发货" value="0"></el-option>
            <el-option label="退款退货" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="应用渠道：" prop="useChannel">
          <el-select v-model="dataForm.useChannel" placeholder="请选择" style="width: 100%">
            <el-option label="请选择" value=""></el-option>
            <el-option label="BBC" value="BBC"></el-option>
            <el-option label="天猫" value="天猫"></el-option>
            <el-option label="京东" value="京东"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="售后截止日期：" prop="afterSaleDate">订单已签收后&nbsp;
          <el-input type="number" v-model="dataForm.afterSaleDate" style="width: 15%" 
          @input="dataForm.afterSaleDate=dataForm.afterSaleDate.replace(/[^\d.]/g,'')" ></el-input>&nbsp;天
        </el-form-item>
      </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button :size="size" @click.native="cancel">{{$t('action.cancel')}}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitForm"
          :loading="editLoading"
        >{{$t('action.submit')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import CyTable from "@/views/Core/ProductTable";
import KtButton from "@/views/Core/KtButton";
import { format } from "@/utils/datetime";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
export default {
  components: {
    KtButton,
    CyTable,
    TableColumnFilterDialog
  },
  computed: {
    numberFomatter: function() {
      return this.dataForm.afterSaleType + "";
    }
  },
  data() {
    var validateAfterSaleDate = (rule, value, callback) => {
      let reg = /^[1-9]\d*$/g;
      if (value == '' && value == null) {
        callback(new Error('请输入售后截止日期'));
      } else if (!reg.test(value)) {
        callback(new Error('售后截止日期应为正整数'));
      } else {
        callback();
      }
    };
    return {
      size: "small",
      filters: {
        code: "",
        name: "",
        status: ""
      },
      columns: [
        {
          prop: "useChannel",
          label: "应用渠道",
          minWidth: 50,
          formatter: function(row, cloum, cellValue) {
            if (cellValue == "") {
              return "默认全渠道";
            } else {
              return cellValue;
            }
          }
        },
        {
          prop: "afterSaleType",
          label: "售后类型",
          minWidth: 120,
          formatter: function(row, cloum, cellValue) {
            if (cellValue == 0) {
              return "退款已发货";
            } else if (cellValue == 1) {
              return "退款退货";
            } else {
              return "-";
            }
          }
        },
        {
          prop: "afterSaleDate",
          label: "售后截止日期",
          minWidth: 120,
          formatter: function(row, cloum, cellValue) {
            return "订单已签收后" + cellValue + "天";
          }
        }
      ],
      pageRequest: {
        pageNum: 1,
        pageSize: 10
      },
      totalSize: 0,
      pageResult: [],
      filterColumns: [],
      editLoading: false,
      operation: false, // true:新增, false:编辑
      editDialogVisible: false, // 新增编辑界面是否显示
      dataFormRules: {
        useChannel: [
          {
            required: true,
            message: "请输入应用渠道",
            trigger: "change"
          }
        ],
        afterSaleType: [
          {
            required: true,
            message: "请输入售后类型",
            trigger: "change"
          }
        ],
        afterSaleDate: [{validator: validateAfterSaleDate, trigger: 'blur'}]
      },
      // 新增编辑界面数据
      dataForm: {
        useChannel: "",
        afterSaleType: "",
        afterSaleDate: ""
      }
    };
  },
  computed: {
    selectOpt: {
      get() {
        return String(this.dataForm.afterSaleType);
      },
      set(val) {
        console.log(val);
        this.dataForm.afterSaleType = Number(val);
      }
    }
  },
  mounted() {
    this.initColumns();
    this.findPage();
  },
  methods: {
    initColumns: function() {
      var temp = [];
      $.each(this.columns, function(key, val) {
        console.log("val:" + val);
        temp.push(val);
      });
      console.log("temp:" + temp);
      this.filterColumns = temp;
    },

    // 获取分页数据
    findPage: function(data) {
      let this_ = this;
      if (this_.filters == undefined || this_.filters == null) {
        this_.filters = {};
      }
      if (data != null) {
        this_.pageRequest.pageNum = data.pageNum;
      }
      this_.filters.start = this.pageRequest.pageNum;
      this_.filters.limit = this.pageRequest.pageSize;
      this.utils.request.queryorderAfterSaleTime(this_.filters, function(res) {
        this_.pageResult = res.data.rows;
        this_.totalSize = Number(res.data.total);
      });
    },
    // 显示新增界面
    handleAdd: function() {
      this.editDialogVisible = true;
      this.operation = true;
      this.dataForm.saleMethod = "";
      this.dataForm.saleType = "";
      this.dataForm.endTime = "";
    },
    // 显示编辑界面
    handleEdit: function(params) {
      console.log(params);
      this.editDialogVisible = true;
      this.operation = false;
      this.$nextTick(function() {
        this.dataForm = Object.assign({}, params.row);
      });
    },
    // 编辑
    submitForm: function() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            let params = Object.assign({}, this.dataForm);
            var this_ = this;
            this.utils.request.editderAfterSaleTime(params, function(res) {
              if (res.code == "0000") {
                this_.$message({
                  message: "操作成功",
                  type: "success"
                });
                this_.$refs["dataForm"].resetFields();
                this_.dataForm = {
                  useChannel: "",
                  afterSaleType: "",
                  afterSaleDate: ""
                };

                this_.editDialogVisible = false;

                this_.findPage(null);
              } else {
                this_.$message({
                  message: "操作失败, " + res.msg,
                  type: "error"
                });
              }
            });
          });
        }
      });
    },
    // 时间格式化
    dateFormat: function(row, column, cellValue, index) {
      return format(row[column.property]);
    },
    // 处理表格列过滤显示
    displayFilterColumnsDialog: function() {
      this.$refs.tableColumnFilterDialog.setDialogVisible(true);
    },
    handleFilterColumns: function(data) {
      this.filterColumns = data.filterColumns;
      this.$refs.tableColumnFilterDialog.setDialogVisible(false);
    },
    //取消编辑窗口
    cancel: function() {
      let this_ = this;
      this_.$refs["dataForm"].resetFields();
      this.editDialogVisible = false;
    }
  }
};
</script>

<style scoped lang='scss'>
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

.box {
  min-height: 500px;
}
</style>
